package com.mall.service.impl;

import com.mall.mapper.BrandMapper;
import com.mall.po.Brand;
import com.mall.po.BrandExample;
import com.mall.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author XueWanglin
 * @ClassName BrandServiceImpl
 * @Description TODO
 * @Date 2021/1/6 19:54
 * @Version 1.0
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired(required = false)
    private BrandMapper brandMapper;
    private BrandExample brandExample = new BrandExample();
    private BrandExample.Criteria criteria = brandExample.createCriteria();

    /**
     * 查询品牌列表
     * @param page
     * @param num
     * @return
     */
    @Override
    public List<Brand> getBrandList(int page, int num) {
//        criteria.andBrandidBetween((page-1)*num,num);

        List<Brand> list = brandMapper.selectBrandList((page-1)*num,num);
        return list;
    }

    /**
     * 查询一个品牌
     * @param brandname
     * @return
     */
    @Override
    public Brand getOneByBrandname(String brandname) {
        criteria.andBrandnameEqualTo(brandname);
        Brand brand = brandMapper.selectByExample(brandExample).get(0);
        return brand;
    }

    /**
     * 增加品牌信息
     * @param brand
     * @return
     */
    @Override
    public Brand addBrandByExample(Brand brand) {
        int result = brandMapper.insertSelective(brand);
        if(result>0){
            criteria.andBrandnameEqualTo(brand.getBrandname());
//            criteria.andBranddescribeEqualTo(brand.getBranddescribe());
            Brand brand1 = brandMapper.selectByExample(brandExample).get(0);
            return brand1;
        }
        return null;
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @Override
    public boolean delBrandById(int id) {
        int result = brandMapper.deleteByPrimaryKey(id);
        return result>0;
    }

    /**
     * 更新品牌信息
     * @param brand
     * @return
     */
    @Override
    public boolean updateBrandByExample(Brand brand) {
        int result = brandMapper.updateByPrimaryKeySelective(brand);
        return result>0;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public boolean delBrandListById(Integer[] ids) {
        int result = brandMapper.delBrandListById(ids);
        return result==ids.length;
    }
}
